package com.runfast.waimai.service.impl;

import com.runfast.common.Constants;
import com.runfast.waimai.dao.model.*;
import com.runfast.waimai.service.*;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

@Slf4j
@Service
public class ShiroServiceImpl implements ShiroService {


    @Autowired
    private BasePurviewService basePurviewService;
    @Autowired
    private BaseUserService baseUserService;
    @Autowired
    private SysUserTokenService sysUserTokenService;
    @Autowired
    private WaiMaiRunfastAgentaccountsService waiMaiRunfastAgentaccountsService;
    @Autowired
    private RunfastBusinessAccountsService runfastBusinessAccountsService;

    @Override
    public BaseUser queryUser(Integer userId) {
        return baseUserService.selectByPrimaryKey(userId);
    }

    @Override
    public WaiMaiRunfastAgentaccounts queryAccounts(Integer userId) {
        return waiMaiRunfastAgentaccountsService.selectByPrimaryKey(userId);
    }
    public RunfastBusinessAccounts queryBusinessAccounts(Integer userId){
        return runfastBusinessAccountsService.selectByPrimaryKey(userId);
    }
    @Override
    public SysUserToken queryByToken(String token) {
        return sysUserTokenService.queryByToken(token);
    }


    @Override
    public Set<String> getUserPermissions(Integer userId) {
        List<String> permsList;

        //超级管理员，拥有最高权限
        if(userId == Constants.SUPER_ADMIN){
            BasePurviewExample basePurviewExample=new BasePurviewExample();
            basePurviewExample.createCriteria().andPtypeBetween(1,2);
            List<BasePurview> menuList = basePurviewService.selectByExample(basePurviewExample);
            permsList = new ArrayList<>(menuList.size());
            for(BasePurview menu : menuList){
                permsList.add(menu.getLink());
            }
        }else{
            //permsList = baseUserService.queryAllPerms(userId);
            permsList=null;
        }
        //用户权限列表
        Set<String> permsSet = new HashSet<>();
        for(String perms : permsList){
            if(StringUtils.isBlank(perms)){
                continue;
            }
            permsSet.addAll(Arrays.asList(perms.trim().split(",")));
        }
        return permsSet;
    }
}